package com.mytrans.client;

import com.mytrans.beans.ReturnObject;
import com.mytrans.beans.TranceRecordBean;
import com.mytrans.util.JsonType;
import com.netflix.loadbalancer.IRule;
import feign.Client;
import feign.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class MytransServerClient {

    @Autowired
    private RibbonClient ribbonClient;

    public ReturnObject<Void> taskSuccess(TranceRecordBean transRecordBean) throws IOException {
        return ribbonClient.execute("/mytrans/taskSuccess", RequestMethod.POST, null,
                transRecordBean, new JsonType<ReturnObject<Void>>() {});
    }

    public ReturnObject<Void> rollBackSuccess(@RequestBody TranceRecordBean transRecordBean) throws IOException {
        return ribbonClient.execute("/mytrans/rollBackSuccess", RequestMethod.POST, null,
                transRecordBean, new JsonType<ReturnObject<Void>>() {});
    }

    public ReturnObject<Void> commitSuccess(@RequestBody TranceRecordBean transRecordBean) throws IOException {
        return ribbonClient.execute("/mytrans/commitSuccess", RequestMethod.POST, null,
                transRecordBean, new JsonType<ReturnObject<Void>>() {});
    }

    public ReturnObject<Void> callRollBack(@PathVariable("token") String token) throws IOException {
        return ribbonClient.execute("/mytrans/callRollBack/" + token, RequestMethod.POST, null,
                null, new JsonType<ReturnObject<Void>>() {});
    }

    public ReturnObject<Void> callCommit(@PathVariable("token") String token) throws IOException {
        return ribbonClient.execute("/mytrans/callCommit/" + token, RequestMethod.POST, null,
                null, new JsonType<ReturnObject<Void>>() {});
    }


}
